package org.meetunexpectedly.service.audit;

import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.util.StringMap;
import org.meetunexpectedly.constant.AuditLabelMap;
import org.meetunexpectedly.constant.AuditStatus;
import org.meetunexpectedly.entity.json.DetailsJson;
import org.meetunexpectedly.entity.json.ResultChildJson;
import org.meetunexpectedly.entity.response.AuditResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @Description: 文本审核服务
 * @Author: pupil
 * @Date: 2024/08/20 下午 4:51
 */
@Service
public class TextAuditService extends AbstractAuditService<String, AuditResponse> {

    // 七牛云文本审核api
    static String textUrl = "http://ai.qiniuapi.com/v3/text/censor";
    // 文本审核请求体模板
    static String textBody = "{\n" +
            "    \"data\": {\n" +
            "        \"text\": \"${text}\"\n" +
            "    },\n" +
            "    \"params\": {\n" +
            "        \"scenes\": [\n" +
            "            \"antispam\"\n" +
            "        ]\n" +
            "    }\n" +
            "}";

    @Override
    public AuditResponse audit(String text) {
        AuditResponse auditResponse = new AuditResponse(AuditStatus.SUCCESS.num,"正常");

        // 判断是否开启审核
        if (!isNeedAudit()) {
            // 没有开启直接返回
            return auditResponse;
        }

        String body = textBody.replace("${text}", text);
        String method = "POST";
        // 获取token
        String token = qiNiuConfig.getAuditToken(textUrl, method, body, contentType);
        StringMap headers = new StringMap();
        headers.put("Host", "ai.qiniuapi.com");
        headers.put("Content-Type", contentType);
        headers.put("Authorization", token);

        // 创建配置并设置文件存储的服务区域
        Configuration cfg = new Configuration(qiNiuConfig.getRegion());
        Client client = new Client(cfg);

        try {
            Response response = client.post(textUrl, body.getBytes(), headers, contentType);
            Map map = objectMapper.readValue(response.getInfo().split("\n")[2], Map.class);
            ResultChildJson result = objectMapper.convertValue(map.get("result"), ResultChildJson.class);
            // 文本审核直接审核 suggestion ，如果返回结果中的 suggestion 不是 pass 的话，就是不通过
            if (!result.getSuggestion().equals("pass")) {
                auditResponse.setAuditStatus(AuditStatus.PASS.num);
                auditResponse.setMsg("");
                List<DetailsJson> details = result.getScenes().getAntispam().getDetails();
                if (!ObjectUtils.isEmpty(details)) {
                    // 遍历所有问题
                    for (DetailsJson detail : details) {
                        if (!detail.getLabel().equals("normal")) {
                            auditResponse.setMsg(AuditLabelMap.getInfo(detail.getLabel()) + "\n");
                        }
                        auditResponse.setFlag(true); // 设置违规状态
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return auditResponse;
    }
}
